<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" :where="defaultWhere" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="userId"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        v-model:selections="selections"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
        :loadOnCreated="false"
      >
        <template #realname="{ row }">
          <span
            :class="genderClass(row.gender)"
            @click="showStudentDetail(row)"
          >
            {{ row.realname }}
          </span>
          <el-tag
            v-if="row.once_blacklist === 1"
            class="pointer"
            style="margin-left: 5px"
            size="small"
            type="info"
            @click="showBlackList(row)"
          >
            黑
          </el-tag>
        </template>
        <template #status="{ row }">
          <el-tag :type="statusType(row.refund)">
            {{ statusText(row.refund) }}
          </el-tag>
        </template>
        <template #jobcate="{ row }">
          {{ row.job_cate }}{{ row.job_other }}
        </template>
        <template #identity="{ row }">
          <span :class="identityClass(row.identity)">
            {{ identityText(row.identity) }}
          </span>
        </template>
        <template #action="{ row }">
          <el-link
            v-if="row.id > 0"
            type="success"
            :underline="false"
            @click="openJudge(row)"
          >
            查看详情
          </el-link>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 回访 -->
    <judge v-model="showJudge" :job="current" type="judge" @done="reload" />
    <!-- 学生详情 -->
    <detail v-model="showDetail" :student-id="currentId" @done="reload" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './search.vue';
  import Judge from '@/views/company/apply/components/pay.vue';
  import Detail from '@/views/student/components/detail.vue';
  import { getJobById } from '@/api/company';
  import { pagePayRecord } from '@/api/money';

  const props = defineProps({
    defaultWhere: {
      type: Object,
      default: () => {}
    },
    action: {
      type: String,
      default: 'list'
    }
  });

  /** 表格实例 */
  const tableRef = ref(null);

  const genderClass = (value) => {
    const ut = {
      0: 'pointer red',
      1: 'pointer blue'
    };
    return ut[value];
  };

  const statusText = (value) => {
    const cs = {
      0: '正常',
      1: '已退款'
    };
    return cs[value];
  };
  const statusType = (state) => {
    const stateMap = {
      0: 'success',
      1: 'danger'
    };
    return stateMap[state];
  };

  /** 表格列配置 */
  const columns = ref([
    {
      slot: 'status',
      label: '状态',
      width: 100,
      align: 'center'
    },
    {
      prop: 'site',
      label: '城市',
      width: 105
    },
    {
      slot: 'realname',
      label: '姓名',
      minWidth: 100
    },
    {
      prop: 'amount',
      label: '金额',
      align: 'center',
      miniWidth: 100
    },
    {
      prop: 'type',
      label: '类型',
      align: 'center',
      miniWidth: 100
    },

    {
      prop: 'pay_time',
      label: '支付时间',
      miniWidth: 150,
      align: 'center'
    },
    {
      prop: 'out_trade_no',
      label: '支付订单号',
      miniWidth: 180,
      align: 'center'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 当前编辑数据 */
  const currentId = ref(null);

  /** 是否显示编辑弹窗 */
  const showJudge = ref(false);

  /** 是否显示编辑弹窗 */
  const showDetail = ref(false);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pagePayRecord({
      ...where,
      ...orders,
      page,
      limit,
      wating_pay: 1
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 打开编辑弹窗 */
  const openJudge = (row) => {
    getJobById({ id: row.id })
      .then((res) => {
        console.log(res);
        current.value = res;
        showJudge.value = true;
      })
      .catch((e) => {
        console.log(e);
      });
  };

  /** 打开编辑弹窗 */
  const showStudentDetail = (row) => {
    currentId.value = row.id ?? 0;
    showDetail.value = true;
  };
</script>

<script>
  export default {
    name: 'SystemUser'
  };
</script>
